package zhcms.framework.dao;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import zhcms.cms.GyTablseToList;
import zhcms.framework.GyReplace;
import zhcms.framework.GySplit;

public class GyList {

	// 得到所有的List标签
		private String returnS;
		private String currnS;
		
		public GyList()
		{
			returnS="";
		}
		
		public void setContent(String content){
			this.currnS=content;
		}
		
		@SuppressWarnings("unused")
		public void ParseList(String gytFrontSeparator,String gytBackSeparator)
		{
			HashMap<String, ArrayList<HashMap<String, String>>> gytInList;
			gytInList = new HashMap<String, ArrayList<HashMap<String, String>>>();
			int Numlist=0;
			int maxDepth=0;
			String SourceHtml="";
			SourceHtml=this.currnS;
			String listhead="";
			String listfoot="";

			String[] tempContent = null;
			
			GyLinkListTables head = null;
			GyLinkListTables CL = new GyLinkListTables();
			
			GySplit a = new GySplit();
			a.tool(this.currnS, gytFrontSeparator+"list:");
			tempContent = a.Show();
			a = null;
			
			//查找是否有for，如果没有的话，跳过以下的步骤
			if(tempContent.length<=1)
			{
				this.returnS=this.currnS;
			}
			else
			{
				//System.out.println("查看源代码1：："+tempContent[(tempContent.length-1)]);
				//修复取出尾部
				String[] tempContentfoot = null;
				GySplit z = new GySplit();
				z.tool(tempContent[(tempContent.length-1)], gytFrontSeparator+"endlist:");
				tempContentfoot = z.Show();
				z = null;
				//System.out.println("查看源代码：："+tempContent[(tempContent.length-1)]);
				//System.out.println("查看源代码1：："+tempContentfoot[1]);
				
				String[] tempContentfoot1 = null;
				GySplit z1 = new GySplit();
				z1.tool(tempContentfoot[1], gytBackSeparator);
				tempContentfoot1 = z1.Show();
				z1 = null;
				
				//System.out.println("查看源代码2：："+tempContentfoot1[0]);
				
				//System.out.println("查看源代码1：："+tempContent[(tempContent.length-1)]);
				//修复取出尾部
				String[] tempContentfoot2 = null;
				GySplit z2 = new GySplit();
				z2.tool(tempContent[(tempContent.length-1)], gytFrontSeparator+"endlist:"+tempContentfoot1[0]+gytBackSeparator);
				tempContentfoot2 = z2.Show();
				z2 = null;
				
				listhead=tempContent[0];
				listfoot=tempContentfoot2[1];
				//取尾巴的内容
				
				//修复成功
				//
				Numlist=tempContent.length-1;
				for(int i=0;i<Numlist;i++)
				{
					String tempcuurS="";
					String keyword="";
					String Content="";
					
					String itables="";
					String inum="0";
					String iorder="desc";
					//
					for(int j=i;j<Numlist;j++)
					{
						tempcuurS=tempcuurS+tempContent[j+1]+gytFrontSeparator+"list:";
					}
					
					//System.out.println("[[["+i+"]]]tempcuurS============"+tempcuurS);
					
					String[] tempContentB = null;
					GySplit b = new GySplit();
					b.tool(tempcuurS, gytBackSeparator);
					tempContentB = b.Show();
					b = null;
					keyword=tempContentB[0];
					//从keyword中提取相应的表，数目，排序规则
					String[] tempTablesA = null;
					GySplit f = new GySplit();
					f.tool(keyword, "=");
					tempTablesA = f.Show();
					f = null;
					itables=tempTablesA[0];
					inum=tempTablesA[1];
					iorder=tempTablesA[2];
					
					
					//获取相应的list
					//注：非本框架中不需要使用这个地方
					GyTablseToList gytlist=new GyTablseToList();
					ArrayList<HashMap<String, String>> gytemplist;
					gytemplist=new ArrayList<HashMap<String, String>>();
					
					gytemplist=gytlist.getAllList(itables, iorder, inum);
					
					gytInList.put(keyword, gytemplist);
					
					//System.out.println("[[["+i+"]]]keyword============"+keyword);
					String[] tempContentC = null;
					GySplit c = new GySplit();
					c.tool(tempcuurS, gytFrontSeparator+"endlist:"+keyword+gytBackSeparator);
					tempContentC = c.Show();
					c = null;
					
					Content=tempContentC[0];
					//System.out.println("[[["+i+"]]]Content============"+Content);
					GyReplace gyr=new GyReplace();
					Content=gyr.replace(Content, keyword+gytBackSeparator, "");
					
					//添加前先便利更新深度
					
					GyLinkListTables htemp = null;
					GyListData tempnodeData ;
					htemp = head;
					
					int cdepth=0;
					
					while(htemp != null)							//循环处理链表每个结点
					{
						tempnodeData = htemp.nodeData;
						
						String[] tempContentD = null;
						GySplit d = new GySplit();
						d.tool(tempnodeData.content, gytFrontSeparator+"list:"+keyword+gytBackSeparator);
						tempContentD = d.Show();
						d = null;
						
						if(tempContentD.length<2)
						{
							
						}
						else
						{
							cdepth=cdepth+1;
							if(maxDepth>cdepth)
							{
								
							}
							else
							{
								maxDepth=cdepth;
							}
						}
						
						htemp = htemp.nextNode;
					}
					
					
					//System.out.println("[[["+i+"]]]Content============"+Content);
					GyListData nodeData = new GyListData();
					nodeData.id=i;
					nodeData.keyword=keyword;
					nodeData.content=Content;
					nodeData.depth=cdepth;
					head = CL.CLAddEnd(head, nodeData);//在链表尾部添加结点
				}
				//System.out.println("[[]]maxDepth============"+maxDepth);
				//CL.CLAllNode(head);
				
				//ArrayList<Map<String, String>> forb=new ArrayList<Map<String, String>>();
				GyListArrayList[] templist;
				templist=new GyListArrayList[(maxDepth+1)];
				for(int fi=maxDepth;fi>=0;fi--)
				{
					//System.out.println("[[[[[]]]]]]]]]]]]]+++++++"+fi);
					GyLinkListTables listhtemp;
					GyListData listnodeData ;
					listhtemp = head;
					GyListArrayList listMain=new GyListArrayList();
					ArrayList<ArrayList<Map<String,String>>> listc=new ArrayList<ArrayList<Map<String,String>>>();
					while(listhtemp != null)							//循环处理链表每个结点
					{
						listnodeData = listhtemp.nodeData;
						//System.out.println("关键词["+fornodeData.keyword+"]");
						if(listnodeData.depth==fi)
						{
							if(listnodeData.depth==maxDepth)
							{
								//是最里层
								//只是循环，不做其它的处理
								ArrayList<Map<String,String>> listb=new ArrayList<Map<String,String>>();
								Map<String,String> lista=new HashMap<String,String>();
								//System.out.println("处理2");
								GyGetList ggf=new GyGetList();
								ggf.setcurrnS(listnodeData.content);
								ggf.ParseList(gytFrontSeparator, gytBackSeparator, gytInList, listnodeData.keyword);
								lista.put("content", ggf.getsContent());
								lista.put("oldcontent", listnodeData.content);
								lista.put("id", listnodeData.id+"");
								lista.put("keyword", listnodeData.keyword);
								lista.put("depth", listnodeData.depth+"");
								listb.add(lista);
								listc.add(listb);
							}
							else
							{
								//如果不是最里层
								//则先处理最和外层的关系
								//首先取得下一层中所有已经替换的数据
								//当前变量
								String cqcontent=listnodeData.content;
								String cqkeyword=listnodeData.keyword;
								int cqid=listnodeData.id;
								int cqdepth=listnodeData.depth;
								
								for(int listti=0;listti<templist[(fi+1)].getListArrayList().size();listti++)
								{
									//判断当前的变量中是否含有下一层中的关键词，即是否替换for变量
									ArrayList<Map<String,String>> ThLista=new ArrayList<Map<String,String>>();
									ThLista=templist[(fi+1)].getListArrayList().get(listti);
									for(int listthi=0;listthi<ThLista.size();listthi++)
									{
										HashMap<String, String> iThLista = (HashMap<String, String>) ThLista.get(listthi);
										
										String thcontent=iThLista.get("content");//替换过的内容，
										String tholdcontent=iThLista.get("oldcontent");//未替换的内容
										String thkeyword=iThLista.get("keyword");//关键词
										//System.out.println("查找"+gytFrontSeparator+"list:" +thkeyword +gytBackSeparator+"");
										String[] tempContentE = null;
										GySplit e = new GySplit();
										e.tool(cqcontent, gytFrontSeparator+"list:" +thkeyword +gytBackSeparator);
										tempContentE = e.Show();
										e = null;
										if(tempContentE.length<2)
										{
											//没有找到
										}
										else
										{
											//找到替换
											//System.out.println("把｛"+tholdcontent+"｝替换成【"+thcontent+"】");
											
											GyReplace r = new GyReplace();
											r.replaceAll(cqcontent, gytFrontSeparator+"list:" +thkeyword +gytBackSeparator+tholdcontent+gytFrontSeparator+"endlist:" +thkeyword +gytBackSeparator, thcontent);
											cqcontent=r.Show();
										}
										
									}
									
									
								}
								
								ArrayList<Map<String,String>> listb=new ArrayList<Map<String,String>>();
								Map<String,String> lista=new HashMap<String,String>();
								//System.out.println("处理2");
								GyGetList ggf=new GyGetList();
								ggf.setcurrnS(cqcontent);
								ggf.ParseList(gytFrontSeparator, gytBackSeparator, gytInList, listnodeData.keyword);
								lista.put("content", ggf.getsContent());
								lista.put("oldcontent", listnodeData.content);
								lista.put("id", listnodeData.id+"");
								lista.put("keyword", listnodeData.keyword);
								lista.put("depth", listnodeData.depth+"");
								listb.add(lista);
								//forMain.setArrayList(fora);
								listc.add(listb);
							}
						}
						
						listhtemp = listhtemp.nextNode;
					}
					listMain.setArrayList(listc);
					templist[fi]=listMain;
				}
				this.returnS=listhead;
				
				//for(int tti=0;tti<(maxDepth+1);tti++)
				//{
					for(int listti=0;listti<templist[0].getListArrayList().size();listti++)
					{
						//判断当前的变量中是否含有下一层中的关键词，即是否替换for变量
						ArrayList<Map<String,String>> ThLista=new ArrayList<Map<String,String>>();
						ThLista=templist[(0)].getListArrayList().get(listti);
						for(int listthi=0;listthi<ThLista.size();listthi++)
						{
							HashMap<String, String> iThLista = (HashMap<String, String>) ThLista.get(listthi);
							
							String thcontent=iThLista.get("content");//替换过的内容，
							String tholdcontent=iThLista.get("oldcontent");//未替换的内容
							String thkeyword=iThLista.get("keyword");//关键词
							if(iThLista.get("depth").equals("0"))
							{
								//System.out.println("开始查看["+0+"(+"+forti+"+)]{"+forthi+"}【"+thkeyword+"】\n"+thcontent+"\n");
								//this.returnS=this.returnS+thcontent;
								GyReplace gyr = new GyReplace();
								SourceHtml=gyr.replace(SourceHtml, gytFrontSeparator+"list:" + thkeyword + gytBackSeparator +tholdcontent + gytFrontSeparator+"endlist:" + thkeyword + gytBackSeparator, thcontent);
								
							}
						}
			
					}
				//}
					
				//this.returnS=this.returnS+listfoot;
					this.returnS=SourceHtml;
				//System.out.println("开始查看\n"+this.returnS+"\n");
			}
		}
		
		
		
		@SuppressWarnings("unused")
		public void ParseList(String gytFrontSeparator,String gytBackSeparator,String gytPFrontSeparator,String gytPBackSeparator)
		{
			HashMap<String, ArrayList<HashMap<String, String>>> gytInList;
			gytInList = new HashMap<String, ArrayList<HashMap<String, String>>>();
			int NumList=0;
			int maxDepth=0;
			
			String SourceHtml="";
			SourceHtml=this.currnS;
			
			String listhead="";
			String listfoot="";
			
			String[] tempContent = null;
			
			GyLinkListTables head = null;
			GyLinkListTables CL = new GyLinkListTables();
			
			GySplit a = new GySplit();
			a.tool(this.currnS, gytFrontSeparator+"list:");
			tempContent = a.Show();
			a = null;
			
			//查找是否有for，如果没有的话，跳过以下的步骤
			if(tempContent.length<=1)
			{
				this.returnS=this.currnS;
			}
			else
			{
				//修复取出尾部
				String[] tempContentfoot = null;
				GySplit z = new GySplit();
				z.tool(tempContent[(tempContent.length-1)], gytFrontSeparator+"endlist:");
				tempContentfoot = z.Show();
				z = null;
				//System.out.println("查看源代码：："+tempContent[(tempContent.length-1)]);
				//System.out.println("查看源代码1：："+tempContentfoot[1]);
				
				String[] tempContentfoot1 = null;
				GySplit z1 = new GySplit();
				z1.tool(tempContentfoot[1], gytBackSeparator);
				tempContentfoot1 = z1.Show();
				z1 = null;
				
				//System.out.println("查看源代码2：："+tempContentfoot1[0]);
				
				//System.out.println("查看源代码1：："+tempContent[(tempContent.length-1)]);
				//修复取出尾部
				String[] tempContentfoot2 = null;
				GySplit z2 = new GySplit();
				z2.tool(tempContent[(tempContent.length-1)], gytFrontSeparator+"endlist:"+tempContentfoot1[0]+gytBackSeparator);
				tempContentfoot2 = z2.Show();
				z2 = null;
				
				listhead=tempContent[0];
				listfoot=tempContentfoot2[1];
				//取尾巴的内容
				
				//修复成功
				
				//
				NumList=tempContent.length-1;
				for(int i=0;i<NumList;i++)
				{
					String tempcuurS="";
					String keyword="";
					String Content="";
					
					String itables="";
					String inum="0";
					String iorder="desc";
					//
					for(int j=i;j<NumList;j++)
					{
						tempcuurS=tempcuurS+tempContent[j+1]+gytFrontSeparator+"list:";
					}
					
					//System.out.println("[[["+i+"]]]tempcuurS============"+tempcuurS);
					
					String[] tempContentB = null;
					GySplit b = new GySplit();
					b.tool(tempcuurS, gytBackSeparator);
					tempContentB = b.Show();
					b = null;
					keyword=tempContentB[0];
					//System.out.println("[[["+i+"]]]keyword============"+keyword);
					String[] tempContentC = null;
					GySplit c = new GySplit();
					c.tool(tempcuurS, gytFrontSeparator+"endlist:"+keyword+gytBackSeparator);
					tempContentC = c.Show();
					c = null;
					
					//从keyword中提取相应的表，数目，排序规则
					String[] tempTablesA = null;
					GySplit f = new GySplit();
					f.tool(keyword, "=");
					tempTablesA = f.Show();
					f = null;
					itables=tempTablesA[0];
					inum=tempTablesA[1];
					iorder=tempTablesA[2];
					//获取相应的list
					//注：非本框架中不需要使用这个地方
					GyTablseToList gytlist=new GyTablseToList();
					ArrayList<HashMap<String, String>> gytemplist;
					gytemplist=new ArrayList<HashMap<String, String>>();
					
					gytemplist=gytlist.getAllList(itables, iorder, inum);
					
					gytInList.put(keyword, gytemplist);
					//结束
					Content=tempContentC[0];
					
					GyReplace gyr=new GyReplace();
					Content=gyr.replace(Content, keyword+gytBackSeparator, "");
					
					//添加前先便利更新深度
					
					GyLinkListTables htemp = null;
					GyListData tempnodeData ;
					htemp = head;
					
					int cdepth=0;
					
					while(htemp != null)							//循环处理链表每个结点
					{
						tempnodeData = htemp.nodeData;
						
						String[] tempContentD = null;
						GySplit d = new GySplit();
						d.tool(tempnodeData.content, gytFrontSeparator+"list:"+keyword+gytBackSeparator);
						tempContentD = d.Show();
						d = null;
						
						if(tempContentD.length<2)
						{
							
						}
						else
						{
							cdepth=cdepth+1;
							if(maxDepth>cdepth)
							{
								
							}
							else
							{
								maxDepth=cdepth;
							}
						}
						
						htemp = htemp.nextNode;
					}
					
					
					//System.out.println("[[["+i+"]]]Content============"+Content);
					GyListData nodeData = new GyListData();
					nodeData.id=i;
					nodeData.keyword=keyword;
					nodeData.content=Content;
					nodeData.depth=cdepth;
					head = CL.CLAddEnd(head, nodeData);//在链表尾部添加结点
				}
				//System.out.println("[[]]maxDepth============"+maxDepth);
				//CL.CLAllNode(head);
				
				//ArrayList<Map<String, String>> forb=new ArrayList<Map<String, String>>();
				GyListArrayList[] templist;
				templist=new GyListArrayList[(maxDepth+1)];
				for(int fi=maxDepth;fi>=0;fi--)
				{
					//System.out.println("[[[[[]]]]]]]]]]]]]+++++++"+fi);
					GyLinkListTables listhtemp;
					GyListData listnodeData ;
					listhtemp = head;
					GyListArrayList listMain=new GyListArrayList();
					ArrayList<ArrayList<Map<String,String>>> listc=new ArrayList<ArrayList<Map<String,String>>>();
					while(listhtemp != null)							//循环处理链表每个结点
					{
						listnodeData = listhtemp.nodeData;
						//System.out.println("关键词["+fornodeData.keyword+"]");
						if(listnodeData.depth==fi)
						{
							if(listnodeData.depth==maxDepth)
							{
								//是最里层
								//只是循环，不做其它的处理
								ArrayList<Map<String,String>> listb=new ArrayList<Map<String,String>>();
								Map<String,String> lista=new HashMap<String,String>();
								//System.out.println("处理2");
								GyGetList ggf=new GyGetList();
								ggf.setcurrnS(listnodeData.content);
								ggf.ParseList(gytPFrontSeparator, gytPBackSeparator, gytInList, listnodeData.keyword);
								lista.put("content", ggf.getsContent());
								lista.put("oldcontent", listnodeData.content);
								lista.put("id", listnodeData.id+"");
								lista.put("keyword", listnodeData.keyword);
								lista.put("depth", listnodeData.depth+"");
								listb.add(lista);
								//forMain.setArrayList(fora);
								listc.add(listb);
							}
							else
							{
								//如果不是最里层
								//则先处理最和外层的关系
								//首先取得下一层中所有已经替换的数据
								//当前变量
								String cqcontent=listnodeData.content;
								String cqkeyword=listnodeData.keyword;
								int cqid=listnodeData.id;
								int cqdepth=listnodeData.depth;
								
								for(int listti=0;listti<templist[(fi+1)].getListArrayList().size();listti++)
								{
									//判断当前的变量中是否含有下一层中的关键词，即是否替换for变量
									ArrayList<Map<String,String>> ThLista=new ArrayList<Map<String,String>>();
									ThLista=templist[(fi+1)].getListArrayList().get(listti);
									for(int listthi=0;listthi<ThLista.size();listthi++)
									{
										HashMap<String, String> iThLista = (HashMap<String, String>) ThLista.get(listthi);
										
										String thcontent=iThLista.get("content");//替换过的内容，
										String tholdcontent=iThLista.get("oldcontent");//未替换的内容
										String thkeyword=iThLista.get("keyword");//关键词
										//System.out.println("查找"+gytFrontSeparator+"list:" +thkeyword +gytBackSeparator+"");
										String[] tempContentE = null;
										GySplit e = new GySplit();
										e.tool(cqcontent, gytFrontSeparator+"list:" +thkeyword +gytBackSeparator);
										tempContentE = e.Show();
										e = null;
										if(tempContentE.length<2)
										{
											//没有找到
										}
										else
										{
											//找到替换
											//System.out.println("把｛"+tholdcontent+"｝替换成【"+thcontent+"】");
											
											GyReplace r = new GyReplace();
											r.replaceAll(cqcontent, gytFrontSeparator+"list:" +thkeyword +gytBackSeparator+tholdcontent+gytFrontSeparator+"endlist:" +thkeyword +gytBackSeparator, thcontent);
											cqcontent=r.Show();
										}
										
									}
									
									
								}
								
								ArrayList<Map<String,String>> listb=new ArrayList<Map<String,String>>();
								Map<String,String> lista=new HashMap<String,String>();
								//System.out.println("处理2");
								GyGetList ggf=new GyGetList();
								ggf.setcurrnS(cqcontent);
								ggf.ParseList(gytPFrontSeparator, gytPBackSeparator, gytInList, listnodeData.keyword);
								lista.put("content", ggf.getsContent());
								lista.put("oldcontent", listnodeData.content);
								lista.put("id", listnodeData.id+"");
								lista.put("keyword", listnodeData.keyword);
								lista.put("depth", listnodeData.depth+"");
								listb.add(lista);
								//forMain.setArrayList(fora);
								listc.add(listb);
							}
						}
						
						listhtemp = listhtemp.nextNode;
					}
					listMain.setArrayList(listc);
					templist[fi]=listMain;
				}
				this.returnS=listhead;
				
				//for(int tti=0;tti<(maxDepth+1);tti++)
				//{
					for(int listti=0;listti<templist[0].getListArrayList().size();listti++)
					{
						//判断当前的变量中是否含有下一层中的关键词，即是否替换for变量
						ArrayList<Map<String,String>> ThLista=new ArrayList<Map<String,String>>();
						ThLista=templist[(0)].getListArrayList().get(listti);
						for(int listthi=0;listthi<ThLista.size();listthi++)
						{
							HashMap<String, String> iThLista = (HashMap<String, String>) ThLista.get(listthi);
							
							String thcontent=iThLista.get("content");//替换过的内容，
							String tholdcontent=iThLista.get("oldcontent");//未替换的内容
							String thkeyword=iThLista.get("keyword");//关键词
							if(iThLista.get("depth").equals("0"))
							{
								//System.out.println("开始查看["+0+"(+"+forti+"+)]{"+forthi+"}【"+thkeyword+"】\n"+thcontent+"\n");
								//this.returnS=this.returnS+thcontent;
								GyReplace gyr = new GyReplace();
								SourceHtml=gyr.replace(SourceHtml, gytFrontSeparator+"for:" + thkeyword + gytBackSeparator +tholdcontent + gytFrontSeparator+"endfor:" + thkeyword + gytBackSeparator, thcontent);

							}
						}
			
					}
				//}
					this.returnS=SourceHtml;
				//this.returnS=this.returnS+listfoot;
			}
		}
		
		public String getcurrnS()
		{
			return this.currnS;
		}
		
		public String getreturnS()
		{
			return this.returnS;
		}

}
